Ext.define('Qms.view.token.TokenInfo', {
    extend: 'Ext.form.Panel',
    alias: 'widget.personinfo',
    id: 'personInfo',
    frame: true,
    //height: 195,
    anchor: '100%',
    bodyPadding: 10,
    
    searchTokens: function(tokenDate, searchKey){
       //Ext.Msg.alert('Search Key', visitingDate+": "+searchKey);
       var tokenStore = Ext.getStore('tokenStore');
       tokenStore.load({
            params: {
                tokenDate: tokenDate,
                searchKey: searchKey
            }
        });
    },
    
    initComponent: function() {
        
        Ext.apply(Ext.form.VTypes,{
           mobile: function(value, field){
               return value.length == 10;  
           },
           mobileText: 'Wrong phone number, please make sure it contains 10 digits', 
           mobileMask: /[\d]/   
        });
        
        var me = this;
        var prefixStore = Ext.create("Qms.data.store.PrefixStore"); 
        var destinationStore = Ext.create("Qms.data.store.DestinationStore"); 
        Ext.applyIf(me, {
            items: [
                {
                    xtype: 'fieldset',
                    title: 'Contact Information',
                    items: [
                        {
                            xtype: 'hidden',
                            fieldLabel: 'Id',
                            name: 'id',
                            labelAlign: 'right',
                            anchor: '100%',
                            allowBlank: true,
                            vtype:'alphanum',
                            msgTarget:'under'
                            //readOnly:true
                        },
                        {
                            xtype: 'hidden',
                            fieldLabel: 'Token No.',
                            name: 'tokenNo',
                            labelAlign: 'right',
                            anchor: '100%',
                            allowBlank: true,
                            vtype:'alphanum',
                            msgTarget:'under'
                            //readOnly:true
                        },
                        {
                            xtype: 'hidden',
                            fieldLabel: 'Token Status',
                            name: 'tokenStatus'
                        },
                        {
                            xtype: 'datefield',
                            fieldLabel: 'Date of Appt.',
                            labelAlign: 'right',
                            format: 'Y-m-d',
                            name: 'appointmentDate',
                            id: 'appointmentDate'
                            
                        },
                        {
                            xtype: 'combo',
                            fieldLabel: 'Destination',
                            labelAlign: 'right',
                            //hiddenName: 'destinationId',
                            name: 'destinationId',
                            store: destinationStore,
                            displayField: 'name',
                            valueField: 'id',
                            triggerAction: 'all',
                            editable: false,
                            allowBlank: false,
                            msgTarget: 'under',
                            blankText: 'Must select the Destination',
                            listeners: {
                                select: function(){
                                    /*
                                    Ext.getStore('tokenStore').load({
                                        params:{
                                            destinationId: this.value
                                        }
                                    });
                                    */
                                }
                            }
                            
                        },
                        {
                            xtype: 'textfield',
                            fieldLabel: 'Mobile No',
                            name: 'mobileNo',
                            labelAlign: 'right',
                            anchor: '100%',
                            allowBlank: false,
                            msgTarget:'under',
                            blankText: 'Must enter the Mobile No.',
                            vtype:'mobile'
                        }
                        
                        /*
                        ,
                        {
                            xtype: 'textfield',
                            fieldLabel: 'Email Id',
                            name: 'emailId',
                            labelAlign: 'right',
                            anchor: '100%',
                            vtype: 'email',
                            vtypeText:'like: shekharkumargupta@gmail.com',
                            allowBlank: false,
                            msgTarget: 'under'
                        }*/
                    ]
                },
                {
                    xtype: 'fieldset',
                    padding: 10,
                    title: 'Person Information',
                    items: [
                        /*
                        {
                            xtype: 'combobox',
                            name: 'prefix',
                            fieldLabel: 'Prefix',
                            labelAlign: 'right',
                            anchor: '100%',
                            store: prefixStore,
                            displayField:'value',
                            valueField:'id',
                            //mode: 'local',
                            triggerAction: 'all',
                            editable: false
                        },
                        */
                        {
                            xtype: 'textfield',
                            name: 'firstName',
                            fieldLabel: 'First Name',
                            labelAlign: 'right',
                            anchor: '100%',
                            msgTarget: 'under',
                            allowBlank: false,
                            blankText: 'Must enter the First Name',
                            minLength: 2,
                            minLengthText: 'First name cann\'t be less than 2 character',
                            maxLength: 20,
                            maxLengthText: 'First name cann\'t be greater than 20 character'
                        },
                        {
                            xtype: 'textfield',
                            name: 'middleInitial',
                            fieldLabel: 'Middle Initial',
                            labelAlign: 'right',
                            anchor: '100%'
                        },
                        {
                            xtype: 'textfield',
                            name: 'lastName',
                            fieldLabel: 'Last Name',
                            labelAlign: 'right',
                            anchor: '100%'
                        },
                        /*
                        {
                            xtype: 'numberfield',
                            name: 'age',
                            fieldLabel: 'Age',
                            labelAlign: 'right',
                            msgTarget: 'under',
                            maxLength: 3,
                            minLength: 1,
                            maxValue: 100,
                            minValue: 1,
                            value: 18,
                            anchor: '100%'
                        },
                        */
                        {
                            dockedItems: [
                                {
                                    xtype: 'toolbar',
                                    id: 'personInfoTB',
                                    dock: 'bottom',
                                    layout: {
                                        align: 'middle',
                                        pack: 'end',
                                        type: 'hbox'
                                    },
                                    items: [
                                        {
                                            //xtype: 'button',
                                            id: 'saveButton',
                                            text: 'Save',
                                            formBind: true,
                                            handler: function(){
                                                var form = this.up('form').getForm();
                                                if(form.isValid()){
                                                    form.submit({
                                                       //url: 'reception/createToken',
                                                       method:'POST',
                                                       waitMsg: 'Submiting form...',
                                                       success: function(form, action){
                                                           var responseData = action.result.data;
                                                           Ext.Msg.alert('Success',"<b>Token No.: </b>"+responseData.tokenNo+"<br/> <b>Name:</b> "+responseData.firstName+" "+responseData.middleInitial+" "+responseData.lastName+"<br/> Date: "+responseData.appointmentDate);
                                                           
                                                           //Ext.getStore('tokenStore').load();
                                                           var appointmentDate = Ext.getCmp('appointmentDate').getRawValue();
                                                           me.searchTokens(appointmentDate, '');
                                                           form.reset();
                                                        },
                                                       failure: function(form, action){
                                                           if(action.failureType == Ext.form.Action.CLIENT_INVALID){
                                                               Ext.Msg.alert("Cannot Submit", "Some fields are still invalid! ");
                                                           }
                                                           if(action.failureType == Ext.form.Action.CONNECT_FAILURE){
                                                               Ext.Msg.alert("Failure","Server communication failure: "+
                                                               action.response.status+' '+action.response.statusText);
                                                           }
                                                           if(action.failuretype == Ext.form.Action.SERVER_INVALID){
                                                               Ext.Mst.alert("Warning", "action.result.errormsg");
                                                           }
                                                       }
                                                    });
                                                }
                                            }
                                        },
                                        {
                                            xtype: 'tbseparator'
                                        },
                                        {
                                            xtype: 'button',
                                            id: 'resetButton',
                                            text: 'Reset',
                                            handler: function() {
                                                this.up('form').getForm().reset();
                                            }
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        });
        //Ext.Msg.alert('Message',PrefixJsonStore);
        me.callParent(arguments);
    }
    
});